<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划编号" prop="planNumber">
        <el-input
          v-model="queryParams.planNumber"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="托运人" prop="shipper">
        <el-input
          v-model="queryParams.shipper"
          placeholder="请输入托运人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input
          v-model="queryParams.consignee"
          placeholder="请输入收货人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划状态" prop="planStatus">
        <el-select v-model="queryParams.planStatus" placeholder="请选择计划状态" clearable>
          <el-option
            v-for="dict in dict.type.plan_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:plans:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:plans:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:plans:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:plans:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="plansList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="计划id" align="center" prop="planId" />
      <el-table-column label="合同id" align="center" prop="contractId" />
      <el-table-column label="计划编号" align="center" prop="planNumber" />
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="计划运输量" align="center" prop="plannedTransportVolume" />
      <el-table-column label="托运人" align="center" prop="shipper" />
      <el-table-column label="发货地址" align="center" prop="shipperAddress" />
      <el-table-column label="收货人" align="center" prop="consignee" />
      <el-table-column label="收货地址" align="center" prop="consigneeAddress" />
      <el-table-column label="计划状态" align="center" prop="planStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.plan_status" :value="scope.row.planStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:plans:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:plans:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="wayBillsAdd(scope.row)"
            v-hasPermi="['system:waybills:add']"
          >创建运单</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运输计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同id" prop="contractId">
          <el-input v-model="form.contractId" placeholder="请输入合同id" />
        </el-form-item>
        <el-form-item label="计划编号" prop="planNumber">
          <el-input v-model="form.planNumber" placeholder="请输入计划编号" />
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称" />
        </el-form-item>
        <el-form-item label="计划运输量" prop="plannedTransportVolume">
          <el-input v-model="form.plannedTransportVolume" placeholder="请输入计划运输量" />
        </el-form-item>
        <el-form-item label="托运人" prop="shipper">
          <el-input v-model="form.shipper" placeholder="请输入托运人" />
        </el-form-item>
        <el-form-item label="发货地址" prop="shipperAddress">
          <el-input v-model="form.shipperAddress" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="收货地址" prop="consigneeAddress">
          <el-input v-model="form.consigneeAddress" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="计划状态" prop="planStatus">
          <el-radio-group v-model="form.planStatus">
            <el-radio
              v-for="dict in dict.type.plan_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改运单对话框 -->
    <el-dialog :title="title" :visible.sync="waybillsOpen" width="500px" append-to-body>
      <el-form ref="waybillsForm" :model="waybillsForm" :rules="waybillsRules" label-width="80px">
        <!-- 隐藏的计划id planId -->
        <el-form-item label="运单号" prop="waybillNumber">
          <el-input v-model="waybillsForm.waybillNumber" placeholder="请输入运单号" />
        </el-form-item>
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="waybillsForm.driverName" placeholder="请输入司机姓名" />
        </el-form-item>
        <el-form-item label="司机联系方式" prop="driverContact">
          <el-input v-model="waybillsForm.driverContact" placeholder="请输入司机联系方式" />
        </el-form-item>
        <el-form-item label="发货人" prop="shipper">
          <el-input v-model="waybillsForm.shipper" placeholder="请输入发货人" />
        </el-form-item>
        <el-form-item label="发货地址" prop="shipperAddress">
          <el-input v-model="waybillsForm.shipperAddress" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input v-model="waybillsForm.consignee" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="收货地址" prop="consigneeAddress">
          <el-input v-model="waybillsForm.consigneeAddress" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="预计开始时间" prop="transportStartTime">
          <el-date-picker clearable
            v-model="waybillsForm.transportStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计送达时间" prop="transportEndTime">
          <el-date-picker clearable
            v-model="waybillsForm.transportEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计送达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划运输量" prop="planVolume">
          <el-input v-model="waybillsForm.planVolume" placeholder="请输入计划运输量" />
        </el-form-item>
        <el-form-item label="实际到达量" prop="actualVolume">
          <el-input v-model="waybillsForm.actualVolume" placeholder="请输入实际到达量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitWaybillsForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlans, getPlans, delPlans, addPlans, updatePlans } from "@/api/system/plans";
import { listWaybills, getWaybills, delWaybills, addWaybills, updateWaybills } from "@/api/system/waybills";

export default {
  name: "Plans",
  dicts: ['plan_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运输计划表格数据
      plansList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      waybillsOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNumber: null,
        planName: null,
        shipper: null,
        consignee: null,
        planStatus: null,
      },
      // 表单参数
      form: {},
      // 计划创建运单参数
      waybillsForm: {},
      // 表单校验
      rules: {
        planNumber: [
          { required: true, message: "计划编号不能为空", trigger: "blur" }
        ],
        planName: [
          { required: true, message: "计划名称不能为空", trigger: "blur" }
        ],
        plannedTransportVolume: [
          { required: true, message: "计划运输量不能为空", trigger: "blur" }
        ],
        shipper: [
          { required: true, message: "托运人不能为空", trigger: "blur" }
        ],
        shipperAddress: [
          { required: true, message: "发货地址不能为空", trigger: "blur" }
        ],
        consignee: [
          { required: true, message: "收货人不能为空", trigger: "blur" }
        ],
        consigneeAddress: [
          { required: true, message: "收货地址不能为空", trigger: "blur" }
        ],
        planStatus: [
          { required: true, message: "计划状态不能为空", trigger: "change" }
        ],
      },
      // 表单校验
      waybillsRules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询运输计划列表 */
    getList() {
      this.loading = true;
      listPlans(this.queryParams).then(response => {
        this.plansList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        contractId: null,
        planNumber: null,
        planName: null,
        plannedTransportVolume: null,
        shipper: null,
        shipperAddress: null,
        consignee: null,
        consigneeAddress: null,
        planStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运输计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids
      getPlans(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运输计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            updatePlans(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlans(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交运单按钮 */
    submitWaybillsForm() {
      this.$refs["waybillsForm"].validate(valid => {
        if (valid) {
          addWaybills(this.waybillsForm).then(response => {
            this.$modal.msgSuccess("新增运单成功");
            this.waybillsOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal.confirm('是否确认删除运输计划编号为"' + planIds + '"的数据项？').then(function() {
        return delPlans(planIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/plans/export', {
        ...this.queryParams
      }, `plans_${new Date().getTime()}.xlsx`)
    },
     /** 新增按钮操作 */
    wayBillsAdd(row) {
      this.reset();
      this.waybillsForm.planId = row.planId;
      this.waybillsOpen = true;
      this.title = "添加运单";
    },
  }
};
</script>
